package com.android.shop.adapter;

import java.util.List;

import com.android.shop.R;
import com.android.shop.bean.Dish;
import com.android.shop.common.AsyncImageLoad;
import com.android.shop.constant.MyConstantID;
import com.android.shop.ui.AddFoodActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DishListActivityAdapter extends BaseAdapter {
	private Context context;
	private List<Dish> listBean;
	private LayoutInflater mInflater;
	private ViewHolder holder;
	
	String result;

	public class ViewHolder {
		TextView tv_dishname;
		TextView tv_price;
		ImageView img_dishphoto;
		Button btn;
	}

	public DishListActivityAdapter(Context handler, List<Dish> listBean) {
		this.context = handler;
		this.listBean = listBean;
		mInflater = LayoutInflater.from(handler);
	}

	public void setListBean(List<Dish> listBean) {
		this.listBean = listBean;
	}

	@Override
	public int getCount() {
		return listBean.size();
	}

	@Override
	public Object getItem(int position) {
		return listBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.dish_list_item, null);
			holder = new ViewHolder();
			holder.tv_dishname = (TextView) convertView
					.findViewById(R.id.tv_dishname);
			holder.tv_price = (TextView) convertView
					.findViewById(R.id.tv_price);
			holder.img_dishphoto = (ImageView) convertView
					.findViewById(R.id.img_dishphoto);
			holder.btn = (Button) convertView.findViewById(R.id.btn_modify);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Dish classItem = listBean.get(position);
		if (classItem != null) {
			holder.tv_dishname.setText(classItem.getDishname());
			holder.tv_price.setText(classItem.getPrice());
			holder.btn.setOnClickListener(new ButtonListener(Integer
					.valueOf(classItem.getDishid())));
			new AsyncImageLoad(context, holder.img_dishphoto,
					MyConstantID.IMAGE_PATH + classItem.getPhoto());
		}
		return convertView;
	}

	class ButtonListener implements OnClickListener {
		private String dishid;
		ButtonListener(int id) {
			dishid = String.valueOf(id);
		}

		@Override
		public void onClick(View v) {
			Log.i("dishid", dishid);
			Intent intent = new Intent();
			intent.setClass(context, AddFoodActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("dishid", dishid);
			intent.putExtras(bundle);
			context.startActivity(intent);
		}
	}
}
